package com.jimikongjian.service;

import com.jimikongjian.dao.IArticle;
import com.jimikongjian.dao.IArticleRoom;
import com.jimikongjian.dao.IRoom;
import com.jimikongjian.models.*;
import com.jimikongjian.service.base.IArticleService;
import com.jimikongjian.service.base.IRoomFollowerService;
import com.jimikongjian.service.base.IRoomService;
import com.jimikongjian.service.base.IUserService;
import com.jimikongjian.service.dao.impl.IParameterImpl;
import com.jimikongjian.service.message.ResponseMessageService;
import com.jimikongjian.service.modal.ArticleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/2.
 */
@Service("articleService")
public class ArticleService implements IArticleService {


    @Autowired
    @Qualifier("iArticleImpl")
    private IArticle iArticleImpl;

    @Autowired
    @Qualifier("roomService")
    private IRoomService roomService;

    @Autowired
    @Qualifier("iRoomImpl")
    private IRoom iRoomImpl;

    @Autowired
    @Qualifier("iArticleRoomImpl")
    private IArticleRoom iArticleRoomImpl;

    @Autowired
    @Qualifier("iParameterImpl")
    private IParameterImpl iParameterImpl;

    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;

    @Autowired
    @Qualifier("userService")
    private IUserService userService;


    @Autowired
    @Qualifier("roomFollowerService")
    private IRoomFollowerService roomFollowerService;

    @Value("${paramId}")
    private int paramId;

    /**
     * 创建文章
     *
     * @param article
     * @return
     */
    @Transactional
    public ResponseMessage createArticle(Article article) {
        if (article.getAuthor() > 0 &&
                article.getAuthorNick() != "" &&
                article.getContent() != "" &&
                article.getTitle() != ""){
            iArticleImpl.insertArticle(article);
            List roomList = article.getRoomList();
            for (int i = 0; i < roomList.size(); i ++){
                ArticleRoom articleRoom = new ArticleRoom();
                articleRoom.setArticleId(article.getId());
                articleRoom.setRoomId((Integer) roomList.get(i));
                iArticleRoomImpl.insertArticleRoom(articleRoom);
            }
            return responseMessageService.makeMessage(Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过文章的id查询文章
     *
     * @param id
     * @return
     */
    public ResponseMessage selectArticleById(int id) {
        if (id > 0){
            Article article = iArticleImpl.getArticleById(id);
            if (article != null){
                return responseMessageService.makeMessage(article, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询是否含有该文章
     *
     * @param id
     * @return
     */
    public boolean hasArticle(int id) {
        if (id > 0){
            Article article = iArticleImpl.getArticleById(id);
            if (article != null){
                return true;
            }
        }
        return false;
    }

    /**
     * 通过空间的id号，查询该空间所有的文章
     *
     * @param roomId
     * @param desc
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectArticleByRoomId(int userId, int roomId, int orderBy, int desc, int start, int length) {
        if (userId > 0 && roomId > 0 && (desc == Article.DESC || desc == Article.ASC) && start >= 0 && length > 0){
            Room room = iRoomImpl.getRoomById(roomId);
            //            这里做一次校验，验证该用户是否有该空间的权限
            if (room.getType() == Room.TYPE_PUBLIC_ROOM || roomFollowerService.hasUser(userId, roomId)){
                List articleList;
                if (orderBy == Article.ORDER_BY_LIKE){
                    articleList = iArticleImpl.getArticleListByRoomIdOrderByLike(roomId,start,length);
                    return responseMessageService.makeMessage(articleList, Message.SUCCESS);
                }
                if (orderBy == Article.ORDER_BY_TIME){
                    if (desc == Article.DESC){
                        articleList = iArticleImpl.getArticleListByRoomIdOrderByTimeDesc(roomId, start, length);
                        return responseMessageService.makeMessage(articleList, Message.SUCCESS);
                    }
                    if (desc == Article.ASC){
                        articleList = iArticleImpl.getArticleListByRoomIdOrderByTimeAsc(roomId, start, length);
                        return responseMessageService.makeMessage(articleList, Message.SUCCESS);
                    }
                }
            }
            return responseMessageService.makeMessage(Message.NO_PERMISSION_ERROR);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过作者的id号，查询作者所写的所有的文章
     *
     * @param author
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectArticleByAuthor(int userId, int author, int start, int length) {
        if (userId > 0 && author > 0 && start >= 0 && length >0 ){
            if (userId == author){
                List<ArticleInfo> articleList = iArticleImpl.getArticleListByAuthor(author, start, length);
                return responseMessageService.makeMessage(articleList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_PERMISSION_ERROR);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过用户的id号，查询用户所关注的空间的所有的文章
     *
     * @param userId
     * @param desc
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectArticleByUserId(int userId, int orderBy, int desc, int start, int length) {
        if (userId > 0 && (orderBy == Article.ORDER_BY_DEFAULT || orderBy == Article.ORDER_BY_TIME || orderBy == Article.ORDER_BY_LIKE)
                && (desc == Article.DESC || desc == Article.ASC)
                && start >= 0 && length > 0){
            if (orderBy == Article.ORDER_BY_DEFAULT){
                List<ArticleInfo> articleList = iArticleImpl.getArticleListByUserIdOrderByValue(userId, start, length);
                return responseMessageService.makeMessage(articleList, Message.SUCCESS);
            }
            if (orderBy == Article.ORDER_BY_TIME){
                if (desc == Article.DESC){
                    List<ArticleInfo> articleList = iArticleImpl.getArticleListByUserIdOrderByTimeDesc(userId, start, length);
                    return responseMessageService.makeMessage(articleList, Message.SUCCESS);
                }
                if (desc == Article.ASC){
                    List<ArticleInfo> articleList = iArticleImpl.getArticleListByUserIdOrderByTimeAsc(userId, start, length);
                    return responseMessageService.makeMessage(articleList, Message.SUCCESS);
                }
            }
            if (orderBy == Article.ORDER_BY_LIKE){
                List<ArticleInfo> articleList = iArticleImpl.getArticleListByUserIdOrderByLike(userId, start, length);
                return responseMessageService.makeMessage(articleList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ARTICLE);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 更新文章收藏数，评论数，分享数和受欢迎权重值
     *
     * @param article
     * @return
     */
    public void updateArticle(Article article) {
        if (article.getLikeCount() > 0
                && article.getCommentCount() > 0
                && article.getUpvoteCount() > 0
                && article.getValue() > 0){
            iArticleImpl.updateArticle(article);
        }

    }

    /**
     * 文章点击查看数自加1
     *
     * @param id
     */
    @Override
    public void updateAddArticleClickCount(int id) {
        if (id > 0){
            iArticleImpl.updateAddArticleClickCount(id);
        }
    }

    /**
     * 一篇文章喜爱的数量自加1
     *
     * @param id
     */
    public void updateAddArticleLikeCount(int id) {
        if (id > 0){
            iArticleImpl.updateAddArticleLikeCount(id);
        }
    }

    /**
     * 一篇文章喜爱的数量自减1
     *
     * @param id
     */
    @Override
    public void updateSubArticleLikeCount(int id) {
        if (id > 0){
            iArticleImpl.updateSubArticleLikeCount(id);
        }
    }

    /**
     * 更新一篇文章评论的数量自加1
     *
     * @param id
     */
    public void updateAddArticleCommentCount(int id) {
        if (id > 0){
            iArticleImpl.updateAddArticleCommentCount(id);
        }
    }

    /**
     * 更新一篇文章评论的数量自减1
     *
     * @param id
     */
    @Override
    public void updateSubArticleCommentCount(int id) {
        if (id > 0){
            iArticleImpl.updateSubArticleCommentCount(id);
        }
    }

    /**
     * 更新一篇文章点赞的数量自加1
     *
     * @param id
     */
    public void updateAddArticleUpvoteCount(int id) {
        if (id > 0){
            iArticleImpl.updateAddArticleUpvoteCount(id);
        }
    }

    /**
     * 更新一篇文章点赞的数量自减1
     *
     * @param id
     */
    @Override
    public void updateSubArticleUpvoteCount(int id) {
        if (id > 0){
            iArticleImpl.updateSubArticleUpvoteCount(id);
        }
    }

    /**
     * 更新一篇文章推荐的权重
     *
     * @param id
     */
    public synchronized void updateArticleValue(int id) {
        if (id > 0){
            Article article = iArticleImpl.getArticleById(id);
            Parameter parameter = iParameterImpl.getParameterById(paramId);
            if (article != null && parameter != null){


                /**
                 *
                 *
                 *
                 * 这里要对文章的推荐权重进行计算
                 *
                 *
                 *
                 *
                 */
            }
        }
    }
}
